package com.ccl.iot.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * 创建者     王浩
 * 创建时间   2017/2/24 9:32
 * 描述	      ${TODO}
 */
public class VisualLocationView extends View {

    private final int DRAW_SPEED = 5;

    private Paint mAnimPaintG, mAnimPaintDG, mBackground;
    private RadialGradient mRadialGradient, mRadialGradient2;
    private int mHeight;
    private int mWidth;

    private int mFullRadius;

    private int mGRaios = 1;
    private int mDGRaios = 0;
    private int mD;
    private int mE;
    private int mXRadius;
    private int mYRadius;


    public VisualLocationView(Context context) {
        this(context, null);
    }

    public VisualLocationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VisualLocationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        mAnimPaintG = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAnimPaintG.setStyle(Paint.Style.FILL);
        mD = Color.parseColor("#88dddddd");
        //        mAnimPaintG.setColor(mD);

        mAnimPaintDG = new Paint(Paint.ANTI_ALIAS_FLAG);
        mAnimPaintDG.setStyle(Paint.Style.FILL);
        mE = Color.parseColor("#88eeeeee");

        mBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackground.setColor(Color.RED);
    }

    public void setBackgroundColor(int color){
        mBackground.setColor(color);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = getHeight();
        mWidth = getWidth();

        //        mHeight = getHeight() - mUser.getHeight();
        //        mWidth = getWidth() - mUser.getWidth();

        mXRadius = mWidth / 2;
        mYRadius = mHeight / 2;

        mFullRadius = mXRadius > mYRadius ? mYRadius : mXRadius;


        mRadialGradient = new RadialGradient(mXRadius, mYRadius, mFullRadius,
                new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

        mRadialGradient2 = new RadialGradient(mXRadius, mYRadius, mFullRadius,
                new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

        mAnimPaintG.setShader(mRadialGradient);
        mAnimPaintDG.setShader(mRadialGradient2);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mXRadius, mYRadius, mFullRadius, mBackground);
        if (mGRaios > mDGRaios) {

//            canvas.drawCircle(mXRadius, mYRadius, mFullRadius, mAnimPaintDG);
            mGRaios += DRAW_SPEED;

            if (mGRaios > mFullRadius) {
                mGRaios = mFullRadius;
            }
            mRadialGradient = new RadialGradient(mXRadius, mYRadius, mGRaios,
                    new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

            mAnimPaintG.setShader(mRadialGradient);
            canvas.drawCircle(mXRadius, mYRadius, mGRaios, mAnimPaintG);

            if (mGRaios >= mFullRadius / 2) {
                mDGRaios += DRAW_SPEED;

                if (mDGRaios > mFullRadius) {
                    mDGRaios = mFullRadius;
                }

                mRadialGradient2 = new RadialGradient(mXRadius, mYRadius, mDGRaios,
                        new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

                mAnimPaintDG.setShader(mRadialGradient2);
                canvas.drawCircle(mXRadius, mYRadius, mDGRaios, mAnimPaintDG);
            }
        } else {
//            canvas.drawCircle(mXRadius, mYRadius, mFullRadius, mAnimPaintG);
            mDGRaios += DRAW_SPEED;

            if (mDGRaios > mFullRadius) {
                mDGRaios = mFullRadius;
            }

            mRadialGradient2 = new RadialGradient(mXRadius, mYRadius, mDGRaios,
                    new int[]{mD, mE}, null, Shader.TileMode.CLAMP);

            mAnimPaintDG.setShader(mRadialGradient2);
            canvas.drawCircle(mXRadius, mYRadius, mDGRaios, mAnimPaintDG);

            if (mDGRaios >= mFullRadius / 2) {
                mGRaios += DRAW_SPEED;

                if (mGRaios > mFullRadius) {
                    mGRaios = mFullRadius;
                }

                mRadialGradient = new RadialGradient(mXRadius, mYRadius, mGRaios,
                        new int[]{mE, mD}, null, Shader.TileMode.CLAMP);

                mAnimPaintG.setShader(mRadialGradient);
                canvas.drawCircle(mXRadius, mYRadius, mGRaios, mAnimPaintG);
            }
        }

        if (mGRaios >= mFullRadius) {
            mGRaios = 0;
        }

        if (mDGRaios >= mFullRadius) {
            mDGRaios = 0;
        }

//        postDelayed(drawTask, 50);
    }

    private Runnable drawTask = new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

}
